package com.fruit;
import java.util.HashMap;
import java.util.Map;

enum FruitType {
    APPLE,
    STRAWBERRY,
    MANGO
}


class FruitPriceConfig {
    private Map<FruitType, Double> prices;
    private boolean strawberryDiscount;
    private boolean overallDiscount;

    public FruitPriceConfig() {
        prices = new HashMap<>();
        // 默认价格
        prices.put(FruitType.APPLE, 8.0);
        prices.put(FruitType.STRAWBERRY, 13.0);
        prices.put(FruitType.MANGO, 20.0);

        strawberryDiscount = false;
        overallDiscount = false;
    }

    public void setStrawberryDiscount(boolean discount) {
        this.strawberryDiscount = discount;
        if (discount) {
            prices.put(FruitType.STRAWBERRY, 13.0 * 0.8); // 草莓8折
        } else {
            prices.put(FruitType.STRAWBERRY, 13.0);
        }
    }

    public void setOverallDiscount(boolean discount) {
        this.overallDiscount = discount;
    }

    public double getPrice(FruitType type) {
        return prices.get(type);
    }

    public boolean hasOverallDiscount() {
        return overallDiscount;
    }
}

class ShoppingCart {
    private Map<FruitType, Integer> items;
    private FruitPriceConfig priceConfig;

    public ShoppingCart(FruitPriceConfig priceConfig) {
        this.items = new HashMap<>();
        this.priceConfig = priceConfig;
    }

    public void addItem(FruitType type, int quantity) {
        if (quantity < 0) {
            throw new IllegalArgumentException("数量不能为负数");
        }
        items.put(type, quantity);
    }

    public double calculateTotal() {
        double total = 0.0;

        // 计算各水果总价
        for (Map.Entry<FruitType, Integer> entry : items.entrySet()) {
            total += priceConfig.getPrice(entry.getKey()) * entry.getValue();
        }

        // 满减优惠
        if (priceConfig.hasOverallDiscount() && total >= 100) {
            total -= 10;
            // total -= (int)total/100*10;
        }

        return total;
    }
}

public class SupermarketCheckoutSystem {
    public static void main(String[] args) {
        //苹果和草莓
        testScenario1();

        // 加芒果
        testScenario2();

        // 草莓打8折
        testScenario3();

        //  满100减10
        testScenario4();
    }

    private static void testScenario1() {
        System.out.println("测试1----------------");
        FruitPriceConfig config = new FruitPriceConfig();
        ShoppingCart cart = new ShoppingCart(config);

        cart.addItem(FruitType.APPLE, 1);
        cart.addItem(FruitType.STRAWBERRY, 2);

        double total = cart.calculateTotal();
        System.out.println("总价: " + total);
        System.out.println();
    }

    private static void testScenario2() {
        System.out.println("测试2----------------");
        FruitPriceConfig config = new FruitPriceConfig();
        ShoppingCart cart = new ShoppingCart(config);

        cart.addItem(FruitType.APPLE, 3);
        cart.addItem(FruitType.STRAWBERRY, 4);
        cart.addItem(FruitType.MANGO, 5);

        double total = cart.calculateTotal();
        System.out.println("总价: " + total);
        System.out.println();
    }

    private static void testScenario3() {
        System.out.println("测试3----------------");
        FruitPriceConfig config = new FruitPriceConfig();
        config.setStrawberryDiscount(true); // 启用草莓折扣
        ShoppingCart cart = new ShoppingCart(config);

        cart.addItem(FruitType.APPLE, 6);
        cart.addItem(FruitType.STRAWBERRY, 7);
        cart.addItem(FruitType.MANGO, 8);

        double total = cart.calculateTotal();
        System.out.println("总价: " + total);
        System.out.println();
    }

    private static void testScenario4() {
        System.out.println("测试4----------------");
        FruitPriceConfig config = new FruitPriceConfig();
        config.setStrawberryDiscount(true); // 草莓8折
        config.setOverallDiscount(true); // 启用满减
        ShoppingCart cart = new ShoppingCart(config);

        // 触发满减
        cart.addItem(FruitType.APPLE, 6);
        cart.addItem(FruitType.STRAWBERRY,6 );
        cart.addItem(FruitType.MANGO, 11);

        double total = cart.calculateTotal();
        System.out.println("总价: " + total);
        System.out.println();

        // 不满100的情况
        ShoppingCart cart2 = new ShoppingCart(config);
        cart2.addItem(FruitType.APPLE, 1);
        cart2.addItem(FruitType.STRAWBERRY, 2);
        cart2.addItem(FruitType.MANGO, 3);

        double total2 = cart2.calculateTotal();
        System.out.println("总价: " + total2);
    }
}
